package py

import com.alibaba.fastjson2.JSON
import com.alibaba.fastjson2.JSONObject
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.File


fun main() {
    val jsonFilePath = "src/main/resources/json/response.json"
    val jsonFile = File(jsonFilePath)
    if (jsonFile.exists()) {
        val jsonContent = jsonFile.readText()
        val jsonObject: JSONObject = JSON.parseObject(jsonContent)
        val client = OkHttpClient()
        val a = "application/json; charset=utf-8".toMediaTypeOrNull()
        val jsonData = mutableMapOf<String, Any>()

        val type = listOf("Unbalance", "Misalignment", "Looseness", 1, 2, 3, 4, 5)
        jsonData["source"] = "wireless"
        jsonData["type"] = type[1]
        jsonData["Vel"] = jsonObject["data"]
        jsonData["fMax"] = 2000
        jsonData["fCut"] = 10
        jsonData["fTarget"] = 25
        jsonData["fTarget1"] = 514.5
        jsonData["fTarget2"] = 24.5
        jsonData["fTarget3"] = 24.5 * 44
        jsonData["fTarget4"] = 2 * 50
        jsonData["fTarget5"] = 24.5 * 13


        jsonData["Vel_alert_mmsRMS"] = 4
        jsonData["Vel_danger_mmsRMS"] = 7

        jsonData["Acc_GMF_alert_gPk"] = 0.3
        jsonData["Acc_GMF_danger_gPk"] = 0.5
        jsonData["Vel_GMF_alert_mmsRMS"] = 4.0
        jsonData["Vel_GMF_danger_mmsRMS"] = 7.0
        jsonData["gE_GMF_alert_gE"] = 3.0
        jsonData["gE_GMF_danger_gE"] = 10.0
        jsonData["gE_GTIF_alert_gE"] = 3.0
        jsonData["gE_GTIF_danger_gE"] = 10.0

        jsonData["gE_alert"] = 3
        jsonData["gE_danger"] = 10
        jsonData["defectType"] = "BPFO"
        jsonData["Acc"] = jsonObject["data"]
        jsonData["gE"] = jsonObject["data"]

        jsonData["Vel_RBPF_alert_mmsRMS"] = 4.0
        jsonData["Vel_RBPF_danger_mmsRMS"] = 7.0
        jsonData["Acc_RBPF_alert_gPk"] = 2.5
        jsonData["Acc_RBPF_danger_gPk"] = 3.75

        jsonData["Vel_2XLF_alert_mmsRMS"] = 4.0
        jsonData["Vel_2XLF_danger_mmsRMS"] = 7.0
        jsonData["gE_2XLF_alert_gE"] = 3
        jsonData["gE_2XLF_danger_gE"] = 10

        jsonData["Vel_VPF_alert_mmsRMS"] = 4.0
        jsonData["Vel_VPF_danger_mmsRMS"] = 7.0
        val jsonString = JSON.toJSONString(jsonData)
        val requestBody = jsonString.toRequestBody(a)
        val request = Request.Builder()
            .url("http://10.1.1.30:5000/")
            .post(requestBody)
            .build()
        runCatching {
            val response = client.newCall(request).execute()
            if (response.isSuccessful) {
                val responseData = response.body.string()
                println(responseData)
//                val gearResult = JSON.parseObject(responseData, GearResult::class.java)
            } else {
                println("HTTP Request Failed with response code: ${response.code}")
            }
        }.onFailure { e -> e.printStackTrace() }
    } else {
        println("文件不存在")
    }
}